define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'chase/refund/index' + location.search,
                    add_url: 'chase/refund/add',
                    edit_url: 'chase/refund/edit',
                    del_url: 'chase/refund/del',
                    multi_url: 'chase/refund/multi',
                    table: 'refunds',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_id', title: __('Order_id')},
                        {field: 'order.service_name', title: __('服务项目')},
                        {field: 'order.total_amount', title: __('订单总额')},
                        {field: 'transaction_id', title: __('Transaction_id')},
                        {field: 'total_fee', title: __('Total_fee'), operate:'BETWEEN'},
                        {field: 'reason', title: __('Reason')},
                        {field: 'remark', title: __('Remark')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, data) {
                                if (data.status == 1) {
                                    return `<a href="javascript:;" class="btn btn-xs btn-danger btn-no" data-toggle="tooltip" title="" data-id="${data.id}" data-table-id="table" data-original-title="拒绝"><i class="fa fa-close"></i></a>
                                        <a href="javascript:;" class="btn btn-xs btn-success btn-yes" data-toggle="tooltip" title="" data-id="${data.id}" data-table-id="table" data-original-title="同意"><i class="fa fa-check"></i></a>
                                    `;
                                } else {
                                    return ``;
                                }
                            }}
                    ]
                ]
            });


            //拒绝
            $('#table').on('click', '.btn-no', function () {
                let id = $(this).attr('data-id');
                layer.prompt({
                    title: '请输入拒绝原因',
                }, function (value, index) {
                    $.get('chase/refund/refuse/ids/' + id + '?remark=' + value, function (res) {
                        if (res.code == 1) {
                            layer.msg('请求成功', {icon: 1});
                        } else {
                            layer.msg('请求失败，请联系管理员', {icon: 2})
                        }
                        layer.close(index);
                        $("#table").bootstrapTable('refresh');
                    })
                })
            });
            //同意
            $('#table').on('click', '.btn-yes', function () {
                let id = $(this).attr('data-id');
                layer.open({
                    title: '同意退款',
                    content: "您确定要同意退款吗？",
                    yes: function () {
                        $.get('chase/refund/agree/ids/' + id, function (res) {
                            if (res.code == 1) {
                                layer.msg('退款成功', {icon: 1});
                            } else {
                                layer.msg(res.msg, {icon: 2})
                            }
                            layer.close();
                            $("#table").bootstrapTable('refresh');
                        })
                    }
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'chase/refund/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'chase/refund/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'chase/refund/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        refuse () {
            Controller.api.bindevent();
        },
        agree () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});